<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid;
        }
    </style>
</head>

<body>
    <table></table>
    <script>
        //1.使用节点操作 在页面上根据数据显示出表格，并且实现删除功能
        let arr = [
            { name: 'Jack', age: 18, gender: '男', del: `<button>删除</button>` },
            { name: 'Rose', age: 20, gender: '女', del: `<button>删除</button>` },
            { name: 'Top', age: 22, gender: '男', del: `<button>删除</button>` },
        ];
        //渲染页面
        function into() {
            let table = document.querySelector('table');
            table.innerHTML = ''
            arr.forEach((v, index) => {
                if (index == 0) {
                    let tr = document.createElement('tr');
                    table.appendChild(tr);
                    for (const key in v) {
                        let td = document.createElement('td');
                        tr.appendChild(td).innerHTML = key;
                    }
                }
                let tr = document.createElement('tr');
                table.appendChild(tr);
                for (const key in v) {
                    let td = document.createElement('td');
                    tr.appendChild(td).innerHTML = v[key];
                }
            });
        }
        //删除功能
        function delStu() {
            for (let i = 0; i < arr.length; i++) {
                document.querySelectorAll('table button')[i].onclick = () => {
                    arr.splice(i, 1)
                    i--;
                    into()
                    delStu();
                }
            }
        }
        into();
        delStu();
    </script>
</body>

</html>